﻿using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.IRepository.ICrew;
using Microsoft.EntityFrameworkCore;
using PowerHub.MES.DTO.Crew.QueryDto;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using PowerHub.MES.DTO.Crew.DTO;

namespace PowerHub.MES.Repository.Crew
{
    /// <summary>
    ///排班计划明细数据实现层
    /// </summary>
    public class Crew_SchedulingPlanDetialRepository : BaseRepository<Crew_SchedulingPlanDetial>, ICrew_SchedulingPlanDetialRepository
    {
        private PowerHubMesContext _repositorySys;
        public Crew_SchedulingPlanDetialRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加排班计划明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateCrew_SchedulingPlanDetialAsync(Crew_SchedulingPlanDetial query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;

            if (AddEntity(query) > 0)
            {
                result.Data = true;
            }

            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionCrew_SchedulingPlanDetialAsync(List<string>? ids)
        {
            var result = new OperationResult<bool>();

            var Departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
            Departments.ForEach(w => w.IsDel = 1);
            result.Data = UpdataEntityRange(Departments) > 0 ? true : false;

            return result;
        }

        /// <summary>
        /// 获取排班计划明细列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Crew_SchedulingPlanDetial>>> GetCrew_SchedulingPlanDetialPageListAsync(QueryCrew_SchedulingPlanDTO query)
        {
            var result = new OperationPageResult<List<Crew_SchedulingPlanDetial>>();

            var currentQuery = GetEntitys().AsNoTracking()
                                            .Include(p=>p.Userinfo)
                                            .Include(p=>p.ProductionTask)
                                            .Include(p => p.WorkStation)
                                                .ThenInclude(p => p.Process)
                                            .Where(p => p.IsDel == 0)
                                            .Where(p => p.Status == 0)
                                            .Where(p => p.Crew_SchedulingPlanId.Equals(query.SchedulingPlanId));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data = await currentQuery.OrderByDescending(g => g.Createtime).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 根据id获取排班计划明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Crew_SchedulingPlanDetial> GetCrew_SchedulingPlanDetialByIdAsync(string id)
        {
            return GetEntitys().AsNoTracking().Include(g=>g.WorkStation).Include(g=>g.ProductionTask).FirstOrDefault(g => g.Id.Equals(id));

        }

        /// <summary>
        /// 更新排班计划明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateCrew_SchedulingPlanDetialAsync(Crew_SchedulingPlanDetial query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;
            if (UpdataEntity(query) > 0)
            {
                result.Data = true;
            }
            return result;
        }

        /// <summary>
        /// 根据明细id更新排班计划明细
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<OperationResult<bool>> UpdateCrew_SchedulingPlanDetialByIdAsync(CrewSchedulingPlanDetialsDtos query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;

            var SchedulingPlanDetial = GetEntitys().AsNoTracking().FirstOrDefault(g=>g.Id.Equals(query.Id));
            SchedulingPlanDetial.Status = query.Status;
            SchedulingPlanDetial.WorkStationId = query.WorkStationId;
            if (UpdataEntity(SchedulingPlanDetial) > 0)
            {
                result.Data = true;
            }
            return result;
        }
    }
}
